package com.kevin.v2.spring.formework.webmvc.servlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.RandomAccessFile;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class KvView {
   private File templateFile;

   private static final String CONTENT_TYPE = "text/html;charset=utf-8";

   public KvView (File templateFile) {
      this.templateFile = templateFile;
   }

   /**
    * 动态绑定数据并返回
    *
    * @param model
    * @param request
    * @param response
    * @throws Exception
    */
   public void render (Map<String, ?> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
      StringBuffer sb = new StringBuffer();
      RandomAccessFile ra = new RandomAccessFile(this.templateFile, "r");
      try {
         String line = null;
         while (null != (line = ra.readLine())) {
            line = new String(line.getBytes("ISO-8859-1"), "utf-8");
            Pattern pattern = Pattern.compile("￥\\{[^\\}]+\\}", Pattern.CASE_INSENSITIVE);
            Matcher matcher = pattern.matcher(line);
            while (matcher.find()) {
               String paramName = matcher.group();
               paramName = paramName.replaceAll("￥\\{|\\}", "");
               Object paramValue = model.get(paramName);
               if (null == paramValue) {
                  continue;
               }
               //要把￥{}中间的这个字符串给取出来 line = matcher.replaceFirst(makeStringForRegExp(paramValue.toString()));
               line = matcher.replaceFirst(makeStringForRegExp(paramValue.toString()));
               sb.append(line);
            }
         }
      } finally {
         ra.close();
      }
      response.setCharacterEncoding("utf-8");
      //response.setContentType(DEFAULT_CONTENT_TYPE);
      response.getWriter().write(sb.toString());
   }


   //处理特殊字符
   public static String makeStringForRegExp (String str) {
      return str.replace("\\", "\\\\")
              .replace("*", "\\*")
              .replace("+", "\\+")
              .replace("|", "\\|")
              .replace("{", "\\{")
              .replace("}", "\\}")
              .replace("(", "\\(")
              .replace(")", "\\)")
              .replace("^", "\\^")
              .replace("$", "\\$")
              .replace("[", "\\[")
              .replace("]", "\\]")
              .replace("?", "\\?")
              .replace(",", "\\,")
              .replace(".", "\\.")
              .replace("&", "\\&");
   }

}
